var contentDiv = document.getElementById('score_div');
var participantsDiv = document.getElementById('participants_div');

// Note that if you click the button several times in succession,
// if the state update hasn't gone through, it will submit the same
// delta again.  The hangout data state only remembers the most-recent
// update.
function buttonClicked1() {
	addVote(1);
}

function buttonClicked2() {
	addVote(2);
}

function buttonClicked3() {
	addVote(3);
}

function buttonClicked4() {
	addVote(4);
}

function buttonClicked5() {
	addVote(5);
}

function addVote(value){
  	console.log("Button clicked");
   	console.log( gapi.hangout.data.getState()['vote'] );
   	var vote = "";

	if ( gapi.hangout.data.getState()['vote'] ) {
		vote = gapi.hangout.data.getState()['vote'];
	}

	var id = gapi.hangout.getParticipantId();
	var name = gapi.hangout.getParticipantById(id).displayName;

	var parsed = vote.split(";");
	var repeatIndex = -1;	
	var i;
	for (i = 0; i < parsed.length; i = i + 1){

		var unit = parsed[i].split(":");
		var thatname = unit[0];
		if (thatname == name) {
			repeatIndex = i;
		} 		
	}
	
	if (repeatIndex == -1) {
		repeatIndex = parsed.length; 
	}
	
	parsed[repeatIndex] = '' + name + ":" + value;
	var newDict = parsed.join(";");
	if (newDict.substring(0,1) == ';'){
		newDict = newDict.substring(1);
	}
	gapi.hangout.data.submitDelta({'vote': newDict});
}
       
function participantsUpdated( participantsArray ) {
	console.log("participants: " + participantsArray);
	participantsDiv.innerHTML = "Participants: " + participantsArray.length;
}

// Sets up callbacks for state change
// You should not set up the state object until you get your first callback.
function init() {
   console.log("init");
   gapi.hangout.data.addStateChangeListener(stateUpdated);
   gapi.hangout.addParticipantsListener(participantsUpdated);
}

// Whenever the shared data is updated, rewrite UI
function stateUpdated(delta, metadata) {
	if(!gapi.hangout.data.getState()['vote']) {
		document.getElementById("score_div").innerHTML = "The count is ???."
	} else {
		var vote = gapi.hangout.data.getState()['vote'];
		var parsed = vote.split(";");
		var i = 0;
		var total = 0;
		var count = 0;
		for (i = 0; i < parsed.length; i = i + 1){
			var pa = parsed[i].split(":");
			total = total + parseInt(pa[1]);
			count = count + 1;
		}
		
		var avg = 0;
		if (count > 0) {
			avg = total / count;
		}
		avg = "" + avg;
		document.getElementById("score_div").innerHTML = "The score is " + avg + ".";
		
	}
}

// Note that the hangouts object is not set up until the gadget loads
gadgets.util.registerOnLoadHandler(init);
       
// Reset value of "count" to 0
function resetCounter(){      
	gapi.hangout.data.submitDelta({'vote': ""});
}

//With the element initially hidden, we can show it slowly:
function clickme1(){
  $('#light1').fadeIn('slow', function() {
    // Animation complete
  });
}

    //With the element initially hidden, we can show it slowly:
function clickme2(){
  $('#light2').fadeIn('slow', function() {
    // Animation complete
  });
}

    //With the element initially hidden, we can show it slowly:
function clickme3(){
  $('#light1').fadeOut('slow', function() {
    // Animation complete
  });
    $('#light2').fadeOut('slow', function() {
    // Animation complete
  });
}
